package com.feeyin.radar.crm.service.impl;

import com.feeyin.radar.commons.Page;
import com.feeyin.radar.crm.dao.CustomerDAO;
import com.feeyin.radar.crm.dao.CustomerShareDAO;
import com.feeyin.radar.crm.model.Customer;
import com.feeyin.radar.crm.service.CustomerCollectService;
import com.feeyin.radar.sys.dao.SaleStageSetDAO;
import com.feeyin.radar.sys.dao.UserDAO;
import com.feeyin.radar.sys.model.SaleStageSet;
import com.feeyin.radar.sys.model.Users;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CustomerCollectServiceImpl
  implements CustomerCollectService
{
  private UserDAO userDAO;
  private CustomerDAO customerDAO;
  private SaleStageSetDAO saleStageSetDAO;
  private CustomerShareDAO customerShareDAO;

  public UserDAO getUserDAO()
  {
    return this.userDAO;
  }

  public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
  }

  public CustomerDAO getCustomerDAO() {
    return this.customerDAO;
  }

  public void setCustomerDAO(CustomerDAO customerDAO) {
    this.customerDAO = customerDAO;
  }

  public SaleStageSetDAO getSaleStageSetDAO() {
    return this.saleStageSetDAO;
  }

  public void setSaleStageSetDAO(SaleStageSetDAO saleStageSetDAO) {
    this.saleStageSetDAO = saleStageSetDAO;
  }

  public CustomerShareDAO getCustomerShareDAO() {
    return this.customerShareDAO;
  }

  public void setCustomerShareDAO(CustomerShareDAO customerShareDAO) {
    this.customerShareDAO = customerShareDAO;
  }

  public List getPageCollectCustomers(Page page) {
    Customer customer = new Customer();
    customer.setCustomerCategory("4");

    List customers = new ArrayList();
    customers = this.customerDAO.getPageCustomersByDynamic(customer, page);
    if (customers != null) {
      for (int i = 0; i < customers.size(); i++) {
        Customer cCustomer = new Customer();
        cCustomer = (Customer)customers.get(i);
        Users user = new Users();
        user = this.userDAO.getUser(cCustomer.getMarketPersonId());
        if (user != null) {
          cCustomer.setMarketPerson(user.getUserName());
        }

        if (cCustomer.getSaleStageId() == null)
          continue;
        SaleStageSet saleStage = new SaleStageSet();
        saleStage = this.saleStageSetDAO.getSaleStageByStageCode(cCustomer.getSaleStageId().toString());
        if (saleStage != null) {
          cCustomer.setSaleStage(saleStage.getStageName());
        }
      }
    }

    return customers;
  }

  public Integer getPageCollectCustomersCount() {
    Customer customer = new Customer();
    customer.setCustomerCategory("4");
    return this.customerDAO.getPageCustomersCountByDynamic(customer);
  }

  public boolean salePersonToCustomer(String[] ids, Integer salePersonId) {
    if (salePersonId == null) return false;
    if ((ids == null) || (ids.length <= 0)) return false;

    Users user = new Users();
    user = this.userDAO.getUser(salePersonId);
    if (user == null) return false;

    Customer customer = new Customer();
    customer.setCustomerCategory("1");
    customer.setSalePersonId(salePersonId);
    customer.setUpdateCustomerDate(new Date());
    customer.setUpdateSaleRecordDate(new Date());
    customer.setUpdateSaleStageDate(new Date());
    customer.setSaleStageId(Integer.valueOf(1));
    customer.setIsFollow("1");
    customer.setCustomerSourceId(Integer.valueOf(2));
    customer.setDeptId(user.getDeptId());

    for (int i = 0; i < ids.length; i++) {
      customer.setId(Integer.valueOf(Integer.parseInt(ids[i])));
      boolean result = false;
      result = this.customerDAO.updateCustomer(customer);
      if (result) {
        this.customerShareDAO.insertCustomerShare(Integer.valueOf(Integer.parseInt(ids[i])), salePersonId, "1");
      }
    }
    return true;
  }
}